//
//  WBTabBar.m
//  CD1505Weibo
//
//  Created by 千锋 on 15/12/29.
//  Copyright (c) 2015年 xiaoqiang. All rights reserved.
//

#import "WBTabBar.h"
#import "WBTabBarButton.h"

@interface WBTabBar ()

@property(nonatomic,strong)NSMutableArray*buttonArray;
@property(nonatomic,strong)WBTabBarButton*seleBtn;
@property(nonatomic,strong)UIButton *plusBtn;
@end
@implementation WBTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton*plusBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:plusBtn];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
         [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        self.plusBtn=plusBtn;
    }
    return self;
}
-(NSMutableArray *)buttonArray
{
    if (_buttonArray==nil) {
        _buttonArray=[NSMutableArray array];
        
    }
    return _buttonArray;
}

-(void)setTabBarItem:(UITabBarItem *)tabBarItem
{
    WBTabBarButton*button=[WBTabBarButton buttonWithType:UIButtonTypeCustom];
    button.tabBarItem=tabBarItem;
  
    [self addSubview:button];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonArray addObject:button];
    if (self.buttonArray.count==1) {
        [self buttonPressed:button];
//        button.selected=YES;
//        _seleBtn=button;
    }
 
}
-(void)layoutSubviews
{
    
    [super layoutSubviews];
    //布局
    CGFloat btnW=CGRectGetWidth(self.frame)/(CGFloat)(self.buttonArray.count+1);
    CGFloat plusW=btnW;
    CGFloat plusH=CGRectGetHeight(self.frame);
    self.plusBtn.frame=CGRectMake(0, 0, plusW, plusH);
    self.plusBtn.center=CGPointMake(CGRectGetWidth(self.frame)*0.5, CGRectGetHeight(self.frame)*0.5);
    for (int i =0; i<self.buttonArray.count+1; i++) {
        
        WBTabBarButton*btn=self.buttonArray[i];
        CGFloat bX=btnW*i;
        if (i) {
            bX+=btnW;
        }
        CGFloat bY=0;
        CGFloat bW=btnW;
        CGFloat bH=CGRectGetHeight(self.frame);
        
       
        btn.frame=CGRectMake(bX, bY, bW, bH);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark -按钮点击
-(void)buttonPressed:(WBTabBarButton*)btn
{
//    for (WBTabBarButton*button in self.buttonArray) {
//        button.selected=NO;
//    }
//    btn.selected=YES;
    _seleBtn.selected=NO;
    btn.selected=YES;
    _seleBtn=btn;
    NSInteger index=[self.buttonArray indexOfObject:btn];
//    if ([_delegate respondsToSelector:@selector(passIndex:)]) {
//        [_delegate passIndex:index];
//    }
    if (_passIndex) {
        _passIndex(index);
    }
    
}

@end
